/*******************************************************************
 * File:         main.c
 * Purpose:      Acronyms translation
 * File purpose: Main control
 * Author:       Richard Butler & Gareth Duncan
 * Date:         15 Mar 2002
 ******************************************************************/

/*
    Acronyms - Translation of Internet and Computer-releated Acronyms
    Copyright  1996-1997-1998-1999-2000-2001-2002,
       Richard Butler & Gareth Duncan

    Thanks are due to Gareth Dukes for the sprites used in this program and
    Gareth Duncan for letting me (Rich) develop this program further.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/

#include "acronyms.h"

int pass = 0;

extern window_data win_array[2];
extern menu_data globmen, help_menu, options_menu, open_menu;
extern _kernel_swi_regs in, out;

char *acronyms[MAX_ACRONYMS+1];
char *acronyms_answers[MAX_ACRONYMS+1];
char *emoticons[MAX_ACRONYMS+1];
char *emoticons_answers[MAX_ACRONYMS+1];
char *computers[MAX_ACRONYMS+1];
char *computers_answers[MAX_ACRONYMS+1];

//***************************************************************************
// MAIN
// The big mamma :-)
//***************************************************************************
int main(void)
{
  // Clear out the acronyms and answers array before they are used
  for(pass = 0; pass <= MAX_ACRONYMS; pass++)
  {
    acronyms[pass]=0;
    acronyms_answers[pass]=0;
    emoticons[pass]=0;
    emoticons_answers[pass]=0;
    computers[pass]=0;
    computers_answers[pass]=0;
  }

  wimp();

  // We Need to release malloc memory from window structures
  for(pass = 0; pass < 2; pass++)
  {
    free(win_array[pass].win_name);
    free(win_array[pass].buffer);
    free(win_array[pass].workspace);
  }

  return 0;
}

//***************************************************************************
// CHECK_ACRONYM
// Find a match
//***************************************************************************
void check_acronym(void)
{
  int match=0; // If that is 1 a match has been found*/
  int string_length; // Holds the length of the string*/
  char *text_pointer; // A pointer to the icons text*/
  char *cp; // A pointer to the character that will be checked to see if it
       	    // is lower case and then converted to upper case if it is

  // Get icon 1's text
  text_pointer=get_ptr(win_array[0].win_handle, 1);
  text_pointer[strlen(text_pointer)] = '\0';

  for(pass = 0; pass <= MAX_ACRONYMS; pass++)
  {
    // If icon 1's text match's a smiley
    if(strcmp(emoticons[pass], text_pointer)==0)
    {
      // Change icon 2's text to the answer
      put_icon_text(emoticons_answers[pass], win_array[0].win_handle, 2);
      match=1; // Set the match interger to show a match has been found
      break; // Break the loop
    }
  }

  if(match == 0) // If a match has not yet been found
  {
    for (cp = text_pointer; cp && *cp; cp++)
    {
      *cp = toupper(*cp);
      // Convert the character pointed to by cp to an upper case character
    }

    // Set the text in icon 1 to the new upper case string
    put_icon_text(text_pointer, win_array[0].win_handle, 1);

    string_length = strlen(text_pointer);
    wimp_set_caret_position((wimp_w)win_array[0].win_handle, 1, 0, 0, -1, (int) strlen(text_pointer));

    for(pass = 0; pass <= MAX_ACRONYMS; pass++)
    {
      // If icon 1's text match's an acronym
      if(strcmp(acronyms[pass], text_pointer)==0)
      {
        // Change icon 2's text to the answer
        put_icon_text(acronyms_answers[pass], win_array[0].win_handle, 2);
        match=1; // Set the match interger to show a match has been found
        break; // Break the loop
      }
    }
  }

  if(match == 0) // If a match has not yet been found
  {
    for (cp = text_pointer; cp && *cp; cp++)
    {
      *cp = toupper(*cp);
      // Convert the character pointed to by cp to an upper case character
    }

    // Set the text in icon 1 to the new upper case string
    put_icon_text(text_pointer, win_array[0].win_handle, 1);

    string_length = strlen(text_pointer);

    wimp_set_caret_position((wimp_w)win_array[0].win_handle, 1, 0, 0, -1, (int) strlen(text_pointer));

    for(pass = 0; pass <= MAX_ACRONYMS; pass++)
    {
      // If icon 1's text match's an acronym
      if(strcmp(computers[pass], text_pointer)==0)
      {
        // Change icon 2's text to the answer
        put_icon_text(computers_answers[pass], win_array[0].win_handle, 2);
        match=1; // Set the match interger to show a match has been found
        break; // Break the loop
      }
    }
  }

  if(match == 0) // If no match is found
  {
    // Cough up :-)
    put_icon_text("Sorry - Not known", win_array[0].win_handle, 2);
  }
}
